package com.example.his.api.schedule;

import com.example.his.api.async.CheckupWorkAsync;
import com.example.his.api.db.dao.CheckupReportDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;

@Component
@Slf4j
public class CheckupReportSchedule {
  @Resource
  private CheckupReportDao checkupReportDao;
  @Resource
  private CheckupWorkAsync checkupWorkAsync;
  
  //每天凌晨1-5点，每隔30分钟检查一次，如果有需要生成报告的预约单，就生成报告
  @Scheduled(cron = "0 0,20,40 1-5 * * ?")
  //  @Scheduled(cron = "0 0/1 * * * ?")
  public void createReport() {
    ArrayList <Integer> list = checkupReportDao.searchWillGenerateReport();
    if(list == null || list.size() == 0) {
      return;
    } else {
      //      让异步线程去生成体检报告
      list.forEach(item -> {
        checkupWorkAsync.createReport(item);
      });
      log.debug("生成了" + list.size() + "个体检报告");
    }
  }
}
